<template>
  <div>
    <el-form
      label-position="right"
      label-width="80px"
      :model="userInfo"
      :rules="rules"
      ref="loginForm"
    >
      <el-form-item label="用户名" prop="userName">
        <el-input v-model="userInfo.userName" type="test"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="userInfo.password" type="password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-radio v-model="radio1" :label="true">记住用户名和密码</el-radio>
        <el-radio v-model="radio1" :label="false">暂不需要</el-radio>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submit">登录</el-button>
        <el-button>重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script lang="ts" setup>
import { useStore } from "@/store";
import { LocalCache } from "@/utils/localCache";
const store = useStore();
import { mixins } from "@/utils/mixins";
import { ElForm } from "element-plus";
import { FormValid } from "@/utils/formValid";
const { reactive, ref } = mixins;
const loginForm = ref<InstanceType<typeof ElForm>>();
let radio1 = ref(true);
const userInfo = reactive({
  userName: LocalCache.getItem("userName") || "",
  password: LocalCache.getItem("password") || "",
});

const rules = reactive({
  userName: [
    {
      required: true,
      message: "请输入用户名",
      trigger: "blur",
    },
    {
      min: 3,
      max: 25,
      message: "长度在 3 到 25之间",
      trigger: "blur",
    },
  ],
  password: [
    {
      required: true,
      message: "请输入密码",
      trigger: "blur",
    },
    {
      min: 6,
      max: 25,
      message: "长度在 6 到 25之间",
      trigger: "blur",
    },
  ],
});

const submit = async () => {
  const res = await FormValid.formRules(loginForm.value!);
  if (res) {
    store.dispatch(store.LSModules.moduleUser.actions.login.toString(), userInfo);
  }
  if (radio1.value) {
    //记住用户名和密码
    LocalCache.setItem("userName", userInfo.userName);
    LocalCache.setItem("password", userInfo.password);
  }
};
</script>
